<template>
  <el-table :data="permission" row-key="id" border style="width: 100%; margin: 30px 0px">
    <el-table-column label="名称" prop="name"></el-table-column>
    <el-table-column label="权限值" prop="code"></el-table-column>
    <el-table-column label="修改时间" prop="updateTime"></el-table-column>
    <el-table-column label="操作" align="center">
      <!-- eslint-disable -->
      <template #="{ row, $index }">
        <el-button type="primary" size="default" v-if="row.level !== 4" @click="handleAdd(row)">
          {{ row.level === 3 ? '添加功能' : '添加菜单' }}
        </el-button>
        <el-button type="primary" size="default" v-if="row.level !== 1" @click="handleEdit(row)">
          编辑
        </el-button>
        <el-popconfirm
          :title="`确定要删除[${row.name}]吗?`"
          v-if="row.level !== 1"
          @confirm="handleDelete(row)"
        >
          <template #reference>
            <el-button type="primary" size="default">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog v-model="dialogVisible" :title="menuParams.id ? '修改菜单' : '添加菜单'">
    <el-form>
      <el-form-item label="名称">
        <el-input placeholder="请输入菜单名称" v-model="menuParams.name"></el-input>
      </el-form-item>
      <el-form-item label="权限">
        <el-input placeholder="请输入权限数值" v-model="menuParams.code"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" size="default" @click="dialogVisible = !dialogVisible">
        取消
      </el-button>
      <el-button type="primary" size="default" @click="confirm">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { ref, onMounted } from 'vue'
//引入ts数据类型
import type { Permission, MenuParams } from '@/api/acl/menu/type'
//引入接口方法
import { reqAllPermission, reqAddOrEditMenu, reqDeleteMenu } from '@/api/acl/menu/index'

let permission = ref<Permission[]>()
let dialogVisible = ref<boolean>(false)
let menuParams = ref<MenuParams>({
  code: '',
  level: 0,
  name: '',
  pid: 0,
})

onMounted(() => {
  getPermission()
})
//获取数据
const getPermission = async () => {
  let result = await reqAllPermission()
  if (result.code === 200) {
    permission.value = result.data
  } else {
    ElMessage({
      type: 'error',
      message: '获取权限数据失败',
    })
  }
}
//添加按钮的回调
const handleAdd = (row: Permission) => {
  dialogVisible.value = !dialogVisible.value
  menuParams.value.pid = row.id
  menuParams.value.level = row.level + 1
  menuParams.value.name = ''
  menuParams.value.code = ''
}
//编辑按钮的回调
const handleEdit = (row: Permission) => {
  dialogVisible.value = !dialogVisible.value
  menuParams.value.id = row.id
  menuParams.value.code = row.code
  menuParams.value.name = row.name
  menuParams.value.level = row.level
  menuParams.value.pid = row.pid
}
//确定按钮的回调
const confirm = async () => {
  let result = await reqAddOrEditMenu(menuParams.value)
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: menuParams.value.id ? '修改成功' : '添加成功',
    })
    dialogVisible.value = !dialogVisible.value
    getPermission()
  } else {
    ElMessage({
      type: 'error',
      message: menuParams.value.id ? '修改失败' : '添加失败',
    })
  }
}
//删除按钮的回调
const handleDelete = async (row: Permission) => {
  let result = await reqDeleteMenu(row.id)
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    getPermission()
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
</script>

<style scoped lang="scss"></style>
